﻿using System.Globalization;
using DevExpress.XtraEditors;

namespace Vendas.Controls
{
    public delegate void ExibirPedidoHandler(object sender, ExibirPedidoEventArgs e);

    public class MesaTileControl : TileItem
    {
        public event ExibirPedidoHandler OnExibirMesa;
        public IPedidoRestauranteItem Pedido { get; set; }

        protected virtual void OnOnExibirMesa(ExibirPedidoEventArgs e)
        {
            ExibirPedidoHandler handler = OnExibirMesa;
            if (handler != null) handler(this, e);
        }

        public MesaTileControl(IPedidoRestauranteItem pedido)
        {
            InitializeComponent();
            //Text = "Mesa";
            Text = pedido.Pedido.Mesa.ToString(CultureInfo.InvariantCulture);
            
            pedido.TileControl = this;
            Pedido = pedido;
            //Text2 = iPedido.Pedido.Local.NomeExibicao;
            pedido.OnPedidoAberto += iPedido_OnPedidoAberto;
            pedido.OnPedidoCancelado += iPedido_OnPedidoCancelado;
            pedido.OnPedidoConfirmado += iPedido_OnPedidoConfirmado;
            pedido.OnPedidoFechado += iPedido_OnPedidoFechado;
        }

        void iPedido_OnPedidoFechado(object sender, PedidoEventArgs e)
        {
            Dispose();
        }

        void iPedido_OnPedidoConfirmado(object sender, PedidoEventArgs e)
        {
            
        }

        void iPedido_OnPedidoCancelado(object sender, PedidoEventArgs e)
        {
            Dispose();
        }

        void iPedido_OnPedidoAberto(object sender, PedidoEventArgs e)
        {
            
        }

        private void InitializeComponent()
        {
            DevExpress.XtraEditors.TileItemElement tileItemElement1 = new DevExpress.XtraEditors.TileItemElement();
            // 
            // MesaTileControl
            // 
            this.AppearanceItem.Hovered.BackColor = System.Drawing.Color.White;
            this.AppearanceItem.Hovered.BackColor2 = System.Drawing.Color.Silver;
            this.AppearanceItem.Hovered.Font = new System.Drawing.Font("Tahoma", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.AppearanceItem.Hovered.ForeColor = System.Drawing.Color.Black;
            this.AppearanceItem.Hovered.Options.UseBackColor = true;
            this.AppearanceItem.Hovered.Options.UseFont = true;
            this.AppearanceItem.Hovered.Options.UseForeColor = true;
            this.AppearanceItem.Normal.BackColor = System.Drawing.Color.White;
            this.AppearanceItem.Normal.BorderColor = System.Drawing.Color.Silver;
            this.AppearanceItem.Normal.Font = new System.Drawing.Font("Tahoma", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.AppearanceItem.Normal.ForeColor = System.Drawing.Color.Black;
            this.AppearanceItem.Normal.Options.UseBackColor = true;
            this.AppearanceItem.Normal.Options.UseBorderColor = true;
            this.AppearanceItem.Normal.Options.UseFont = true;
            this.AppearanceItem.Normal.Options.UseForeColor = true;
            this.AppearanceItem.Selected.BackColor = System.Drawing.Color.White;
            this.AppearanceItem.Selected.BorderColor = System.Drawing.Color.Silver;
            this.AppearanceItem.Selected.Font = new System.Drawing.Font("Tahoma", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.AppearanceItem.Selected.ForeColor = System.Drawing.Color.Black;
            this.AppearanceItem.Selected.Options.UseBackColor = true;
            this.AppearanceItem.Selected.Options.UseBorderColor = true;
            this.AppearanceItem.Selected.Options.UseFont = true;
            this.AppearanceItem.Selected.Options.UseForeColor = true;
            this.AppearanceItem.Selected.Options.UseImage = true;
            this.AppearanceItem.Selected.Options.UseTextOptions = true;
            this.BackgroundImageAlignment = DevExpress.XtraEditors.TileItemContentAlignment.BottomCenter;
            this.BackgroundImageScaleMode = DevExpress.XtraEditors.TileItemImageScaleMode.Stretch;
            tileItemElement1.Image = global::Vendas.Properties.Resources.mesa1;
            tileItemElement1.TextAlignment = DevExpress.XtraEditors.TileItemContentAlignment.TopCenter;
            this.Elements.Add(tileItemElement1);
            this.ItemClick += new DevExpress.XtraEditors.TileItemClickEventHandler(this.MesaTileControl_ItemClick);

        }

        private void MesaTileControl_ItemClick(object sender, TileItemEventArgs e)
        {
            ExibirMesa();
        }

        public void ExibirMesa()
        {
            if (OnExibirMesa != null)
            {
                OnExibirMesa(this, new ExibirPedidoEventArgs { PedidoControl = Pedido.TelaPedidoControl });
            }
        }

        public void TrocarMesa(int mesa)
        {
            Pedido.Pedido.Mesa = mesa;
            Text = mesa.ToString(CultureInfo.InvariantCulture);
        }
    }
}
